package com.liushuai.phoneguardian.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.ProgressDialog;

public class DownLoadUtil {

	/**
	 * 获取服务器文件的名称
	 * 
	 * @param serverPath
	 * @return
	 */
	public static String getFileName(String serverPath) {
		return serverPath.substring(serverPath.lastIndexOf("/") + 1);
	}

	public static File download(String serverPath, String savedPath,
			ProgressDialog pd) {
		try {
			URL url = new URL(serverPath);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("GET");
			conn.setConnectTimeout(5000);
			int code = conn.getResponseCode();
			if (code == 200) {
				pd.setMax(conn.getContentLength());
				InputStream is = conn.getInputStream();
				File file = new File(savedPath);
				FileOutputStream fos = new FileOutputStream(file);
				byte[] buffer = new byte[1024];
				int len = 0;
				int total = 0;
				while ((len = is.read(buffer)) != -1) {
					fos.write(buffer, 0, len);
					total += len;
					pd.setProgress(total);
					Thread.sleep(20);
				}
				fos.flush();
				fos.close();
				is.close();
				return file;

			} else {
				return null;
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			return null;
		}
	}
}
